package com.planet.common.autoconfigure.email;


import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Description: TODO 自动配置类
 * @Author: mis_wu
 * @Date: 2023/8/25
 * @Company SMS
 * 使用时必须在配置类中开启 spring.planet.email.enable = true
 **/

@Configuration(proxyBeanMethods = false)
//判断是否是web 工程，是则配置
@ConditionalOnWebApplication
//需要注入哪些类到容器中
@EnableConfigurationProperties(PlanetEmailProperties.class)
@ConditionalOnProperty(prefix = "spring.planet.email",name = "enable",havingValue = "true", matchIfMissing = false)
public class PlanetEmailAutoconfiguration {

    @Resource
    private PlanetEmailProperties planetEmailProperties;

    /**
     * todo 邮件和短信接口一致，在获取bean时，可以指定bean名称取
     * @Qualifier(value = "planetSmsProducer")
     * @return Send
     */
    @Bean(name = "planetEmailProducer")
    @ConditionalOnMissingBean
    public PlanetEmailSender emailProducer(){
        PlanetEmailSender planetEmailSender = new PlanetEmailSender();
        planetEmailSender.setPlanetEmailProperties(planetEmailProperties);
        return planetEmailSender;
    }
}
